package org.rich.charlesmurphy;

import static org.rich.charlesmurphy.core.CMIds.DOWNLOAD_SPEED;
import static org.rich.charlesmurphy.core.CMIds.ELAPSED_TIME;
import static org.rich.charlesmurphy.core.CMIds.EXCEPTION;
import static org.rich.charlesmurphy.core.CMIds.METHOD;
import static org.rich.charlesmurphy.core.CMIds.MIME_TYPE;
import static org.rich.charlesmurphy.core.CMIds.REQUEST_BODY_SIZE;
import static org.rich.charlesmurphy.core.CMIds.REQUEST_HEADER_SIZE;
import static org.rich.charlesmurphy.core.CMIds.RESPONSE_BODY_SIZE;
import static org.rich.charlesmurphy.core.CMIds.RESPONSE_HEADER_SIZE;
import static org.rich.charlesmurphy.core.CMIds.START_TIME;
import static org.rich.charlesmurphy.core.CMIds.STATUS;
import static org.rich.charlesmurphy.core.CMIds.URL;

import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.UIManager;

import org.rich.charlesmurphy.core.DataLoader;
import org.rich.charlesmurphy.core.DataLoaderCommand;
import org.rich.charlesmurphy.core.InputFileType;
import org.rich.charlesmurphy.core.parser.AccessLogParser;
import org.rich.charlesmurphy.core.parser.CsvParser;
import org.rich.charlesmurphy.core.transformer.AccessLogTransformer;
import org.rich.charlesmurphy.core.transformer.CharlesTransformer;
import org.rich.charlesmurphy.ui.View;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class Main implements Runnable {

	static Logger log = LoggerFactory.getLogger(Main.class);

	static String[] CHARLES_CSV_COLUMN_MAPPING = {
		URL,
		METHOD,
		STATUS,
		START_TIME,
		REQUEST_HEADER_SIZE,  // 5
		REQUEST_BODY_SIZE,
		RESPONSE_HEADER_SIZE,
		RESPONSE_BODY_SIZE,
		ELAPSED_TIME,
		DOWNLOAD_SPEED,      // 10
		MIME_TYPE,
		EXCEPTION
	};

//	public static void main(String[] args) {
//		AccessLogParser alp = new AccessLogParser("");
//		String file = "C:\\Documents and Settings\\Rich.O'Connell\\My Documents\\charles\\apacheAccess_trimmed.log";
//
//		File inFile = new File(file);
//
//		ParseResult pr = alp.parse(inFile);
//
//		AccessLogTransformer tx = new AccessLogTransformer();
//
//		DataSet<Request> rr = tx.transform(pr.getDataSet(), null);
//
//		log.debug("Done transform:");
//		for (int i=0; i < rr.size();i++) {
//			log.debug(rr.exportRecord(i).toString());
//		}
//	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
		} catch (Exception e) {
			log.error("Error setting look and feel. Using default.", e);
		}

		EventQueue.invokeLater(new Main());

	}

	public void run() {
		Properties.load(Main.class.getResourceAsStream("/charliemurphy.properties"));

		DataLoader dl = new DataLoader();

		dl.addInputProcessor(InputFileType.CHARLES,
				new CsvParser(CHARLES_CSV_COLUMN_MAPPING),
				new CharlesTransformer());

		dl.addInputProcessor(InputFileType.ACCESS_LOG,
				new AccessLogParser(""),
				new AccessLogTransformer());



		View v = new View();
		v.setDataLoader(dl);

		// load the default

		// String file = args[0];
//		String file = "C:\\Documents and Settings\\Rich.O'Connell\\My Documents\\charles\\IE_memSesh_8_29.csv";
		String file = "C:\\Documents and Settings\\Rich.O'Connell\\My Documents\\charles\\logs\\apacheAccess_trimmed.log";
//		final String host = "patrickstar";
		File inFile = new File(file);
		if (inFile.exists()) {
			v.loadData(new DataLoaderCommand(new File[]{ inFile }, InputFileType.ACCESS_LOG, "Apache Trimmed", "", false));

		}


		v.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				System.exit(0);
			}
		});

	}

}
